﻿Imports System.IO
Imports System.Xml.Serialization

Public Class ApplicationObject

    Public Enum enmGeneratorType
        VisualBasic
        CSharp
        VisualBasicAndCSharp
    End Enum

    Private _VerbatimOrder As Boolean = True
    Public Property VerbatimOrder() As Boolean
        Get
            Return _VerbatimOrder
        End Get
        Set(ByVal value As Boolean)
            _VerbatimOrder = value
        End Set
    End Property

    Private _BlankLinesBetweenMembers As Boolean = True
    Public Property BlankLinesBetweenMembers() As Boolean
        Get
            Return _BlankLinesBetweenMembers
        End Get
        Set(ByVal value As Boolean)
            _BlankLinesBetweenMembers = value
        End Set
    End Property

    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Private _ApplicationDirctory As String
    Public Property ApplicationDirectory() As String
        Get
            Return _ApplicationDirctory
        End Get
        Set(ByVal value As String)
            _ApplicationDirctory = value
        End Set
    End Property

    Public Function GetExportDirectory() As String
        Dim myApplicationDirectory As String = ApplicationDirectory
        'check for absolut path
        If (myApplicationDirectory.Substring(1, 1) = ":"c) OrElse (myApplicationDirectory.StartsWith("\\")) Then Return myApplicationDirectory
        'relativ path
        Dim myCurrentAssemblyPath As String = My.Application.Info.DirectoryPath
        If (Not myCurrentAssemblyPath.EndsWith("\"c)) Then myCurrentAssemblyPath &= "\"c & myApplicationDirectory
        If (Not myCurrentAssemblyPath.EndsWith("\"c)) Then myCurrentAssemblyPath &= "\"c
        Return myCurrentAssemblyPath
    End Function

    Private _GeneratorType As enmGeneratorType = enmGeneratorType.VisualBasic
    Public Property GeneratorType() As enmGeneratorType
        Get
            Return _GeneratorType
        End Get
        Set(ByVal value As enmGeneratorType)
            _GeneratorType = value
        End Set
    End Property

    Private _ClassObjects As List(Of ClassObject)
    Public Property ClassObjects() As List(Of ClassObject)
        Get
            Dim myClassObjects As List(Of ClassObject) = _ClassObjects
            If myClassObjects Is Nothing Then
                myClassObjects = New List(Of ClassObject)
                _ClassObjects = myClassObjects
            End If
            Return myClassObjects
        End Get
        Set(ByVal value As List(Of ClassObject))
            _ClassObjects = value
        End Set
    End Property

    Public Function GetClassObjectByName(ByVal aName As String) As ClassObject
        For Each myClassObject As ClassObject In ClassObjects
            If myClassObject.Name.ToLowerInvariant = aName.ToLowerInvariant Then Return myClassObject
        Next
        Return Nothing
    End Function

    Public Sub SaveApplicationObject(ByVal aXMLFile As String)
        Dim myFile As System.IO.StreamWriter = Nothing
        Try
            Dim myXMLWriter As New XmlSerializer(GetType(ApplicationObject))
            myFile = New System.IO.StreamWriter(aXMLFile)
            myXMLWriter.Serialize(myFile, Me)

        Catch ex As Exception
            Throw New Exception(ex.Message, ex.InnerException)
        Finally
            If Not myFile Is Nothing Then myFile.Close()

        End Try
    End Sub

    Public Function GetClassObject(ByVal aClassName As String) As ClassObject
        Dim myName As String = aClassName.ToLowerInvariant
        For Each myClassObject As ClassObject In ClassObjects
            Dim myKey As String = myClassObject.Name.ToLowerInvariant
            If myKey = myName Then Return myClassObject
        Next
        Return Nothing
    End Function

    Public Shared Function CreateApplicationObjectFromXML(ByVal aXMLFile As String) As ApplicationObject
        Dim myApplicationObject As New ApplicationObject
        Dim myFileStream As FileStream = Nothing
        Try
            ' To read the file, create a FileStream.
            myFileStream = New FileStream(aXMLFile, FileMode.Open)
            myApplicationObject = GenericDeserializer(Of ApplicationObject).DeserializeFromXML(anFileStream:=myFileStream)

        Catch ex As Exception
            Throw New Exception(ex.Message, ex.InnerException)
        Finally
            If myFileStream IsNot Nothing Then myFileStream.Close()
        End Try
        Return myApplicationObject
    End Function

End Class
